package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.web.pojo.CheckBox;
import com.cy.pj.sys.dao.CGBRolesDao;
import com.cy.pj.sys.dao.CGBRolesMenuDao;
import com.cy.pj.sys.pojo.CGBRoles;
import com.cy.pj.sys.service.CGBRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = false,
        rollbackFor = Throwable.class,
        isolation = Isolation.READ_COMMITTED, timeout = 60)
@Service
public class CGBRolesServiceImpl implements CGBRolesService {
    @Autowired
    private CGBRolesDao cgbRolesDao;
    @Autowired
    private CGBRolesMenuDao cgbRolesMenuDao;

    @Transactional(readOnly = true)
    @Override
    public List<CGBRoles> findRoles(CGBRoles cgbRoles) {
        return cgbRolesDao.selectRoles(cgbRoles);
    }

    @Transactional(readOnly = true)
    @Override
    public CGBRoles findById(Integer id) {
        CGBRoles cgbRoles = cgbRolesDao.selectById(id);
        if (cgbRoles == null) throw new ServiceException("记录可能已经不存在");

        return cgbRoles;
    }

    @Override
    public int saveRoles(CGBRoles cgbRoles) {
        //1.参数校验(自己校验)
        //2.保存角色自身信息
        int rows = cgbRolesDao.insertRoles(cgbRoles);
        //3.保存角色和菜单关系数据
//        cgbRolesMenuDao.insertRolesMenu(cgbRoles.getId(), cgbRoles.getMenuIds());
        return rows;
    }

    @Override
    public int updateRoles(CGBRoles cgbRoles) {
        //1.参数校验(自己校验)
        //2.保存角色自身信息
        int rows = cgbRolesDao.updateRoles(cgbRoles);
        if (rows == 0) throw new ServiceException("记录可能不存在");
        //3.更新角色和菜单关系数据
//        cgbRolesMenuDao.deleteRoleId(cgbRoles.getId());
//        cgbRolesMenuDao.insertRolesMenu(cgbRoles.getId(), cgbRoles.getMenuIds());
        return rows;
    }

    @Transactional(readOnly = true)
    @Override
    public List<CheckBox> findCheckRoles() {
        return cgbRolesDao.selectCheckRoles();
    }
}
